<template>
  <view>
    <u--form
      labelWidth="90"
      labelAlign="center"
      :model="highsea"
      :rules="rules"
      ref="uForm"
    >
      <u-form-item label="客户名称" prop="busname" borderBottom ref="item1">
        <u--input v-model="highsea.busname" border="none" disabled></u--input>
      </u-form-item>

      <u-form-item
        label="分配人员"
        prop="admin"
        borderBottom
        @click="showSex = true"
        ref="item1"
      >
        <u--input
          v-model="highsea.admin"
          disabled
          disabledColor="#ffffff"
          placeholder="请选择分配人员"
          border="none"
        ></u--input>
        <u-icon slot="right" name="arrow-right"></u-icon>
      </u-form-item>
    </u--form>

    <u-action-sheet
      :show="showSex"
      :actions="actions"
      title="请选择人员"
      @close="showSex = false"
      @select="sexSelect"
    >
    </u-action-sheet>

    <u-button type="primary" @click="submit">提交</u-button>
    <u-toast ref="uToast"></u-toast>
  </view>
</template>
<script>
export default {
  data() {
    return {
      showSex: false,
      highsea: {
        admin: "",
        adminid: "",
        busname: "",
      },
      actions: [],
      rules: {
        admin: [
          {
            required: true,
            message: "请选择分配人员",
            trigger: ["blur", "change"],
          },
        ],
      },
      switchVal: false,
      radio: "",
      busid: 0,
    };
  },
  onLoad(options) {
    this.busid = options.busid;
    this.assignData();
  },
  methods: {
    // 获取分配人员数据
    async assignData() {
      let result = await this.$u.api.manage.adminlist({ busid: this.busid });

      if (result && result.code === 1) {
        result.data.admin.forEach((item) => {
          this.actions.push({
            name: item.username,
            value: item.id,
          });
        });
        this.highsea.busname = result.data.business.nickname;
      }
    },
    sexSelect(e) {
      this.highsea.admin = e.name;
      this.highsea.adminid = e.value;
      this.showSex = false;
      console.log(this.highsea);
    },
    async submit() {
      let data = {
        busid: this.busid,
        adminid: this.highsea.adminid,
      };

      let result = await this.$u.api.manage.recovery(data);

      if (result && result.code === 1) {
        this.$refs.uToast.show({
          type: "success",
          message: result.msg,
          complete: () => {
            this.$u.route('/pages/manage/highsea/index');
          },
        });
      } else {
        this.$refs.uToast.show({
          type: "error",
          message: result.msg,
        });
      }
    },
  },
  onReady() {
    //如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
    this.$refs.uForm.setRules(this.rules);
  },
};
</script>
